////////////////////////////////////////////////
// File: MeminfoController.java
// Description: Enter summary here after generation.
// Date: 2016-09-23 09:09:34
// Web: http://www.elanzhou.com/
////////////////////////////////////////////////
package com.mvc.controller;

import java.io.IOException;
import java.math.BigInteger;
import java.util.List;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.mvc.entity.Goods;
//import com.mvc.entity.User;
//import com.mvc.entity.UserDetail;
import com.mvc.entity.Meminfo;
import com.mvc.service.DictService;
import com.mvc.service.MeminfoService;
import com.pub.MyJson;
import com.pub.Page;
import com.aspose.cells.Cells;
import com.aspose.cells.Workbook;
import com.aspose.cells.Worksheet;

@Controller
@RequestMapping("Meminfo")
public class MeminfoController {
	protected final transient Log logger = LogFactory.getLog(MeminfoController.class);
	@Autowired
	private MeminfoService meminfoService;
	@Autowired
	private DictService dictService;

	public MeminfoController() {
	}

	/* 列表 */
	@RequestMapping("Index.do")
	public String Index(HttpSession session, ModelMap modelMap, String pageNo, String Ajax) {
		Meminfo smeminfo = (Meminfo) session.getAttribute("SessionMem");
		modelMap.put("smeminfo", smeminfo);
		Page page = new Page();
		page.setPageNox(pageNo);
		page.setPageSize(15);
		String hql = "select a from Meminfo a where a.Status='已审核' order by a.ID desc";
		List list = meminfoService.getMeminfoList(hql, page);
		String counthql = "select count(*) from Meminfo a where a.Status='已审核'";
		page.setPageCount(meminfoService.getCount(counthql, page));
		modelMap.put("page", page);
		modelMap.put("list", list);
		if (Ajax != null && Ajax.equals("Ajax")) {
			return "/Meminfo/List";
		} else {
			return "/Meminfo/Index";
		}

	}

	/* 列表 */
	@RequestMapping("Wsh.do")
	public String Wsh(HttpSession session, ModelMap modelMap, String pageNo, String Ajax) {
		Meminfo smeminfo = (Meminfo) session.getAttribute("SessionMem");
		modelMap.put("smeminfo", smeminfo);
		Page page = new Page();
		page.setPageNox(pageNo);
		page.setPageSize(15);
		String hql = "select a from Meminfo a where a.Status='未审核' order by a.ID desc";
		List list = meminfoService.getMeminfoList(hql, page);
		String counthql = "select count(*) from Meminfo a where a.Status='未审核'";
		page.setPageCount(meminfoService.getCount(counthql, page));
		modelMap.put("page", page);
		modelMap.put("list", list);
		if (Ajax != null && Ajax.equals("Ajax")) {
			return "/Meminfo/List";
		} else {
			return "/Meminfo/Wsh";
		}

	}

	/* 添加 */
	@RequestMapping("Add.do")
	public String Add(HttpSession session, ModelMap modelMap) {
		Meminfo smeminfo = (Meminfo) session.getAttribute("SessionMem");
		modelMap.put("smeminfo", smeminfo);
		List hylist = dictService.getAllByKeyname("角色");
		modelMap.put("hylist", hylist);
		return "/Meminfo/Add";
	}

	/* 保存 */
	@RequestMapping("Save.do")
	public void Save(HttpServletResponse response, HttpServletRequest request, ModelMap modelMap, HttpSession session) {
		Meminfo smeminfo = (Meminfo) session.getAttribute("SessionMem");
		modelMap.put("smeminfo", smeminfo);
		try {

			Meminfo meminfo = new Meminfo();
			boolean tag = true;
			String Msg = "信息保存成功";

			if (tag) {

				String FieldMemname = (String) request.getParameter("Memname");

				if (((FieldMemname != null) & (!FieldMemname.trim().equals("")))) {
					FieldMemname = FieldMemname.trim();
					meminfo.setMemname(FieldMemname);

				} else {
					tag = false;
					Msg = "会员名称不能为空！";
				}
			}

			if (tag) {

				String FieldMempass = (String) request.getParameter("Mempass");

				if (((FieldMempass != null) & (!FieldMempass.trim().equals("")))) {
					FieldMempass = FieldMempass.trim();
					meminfo.setMempass(FieldMempass);

				} else {
					tag = false;
					Msg = "会员密码不能为空！";
				}
			}

			if (tag) {

				String FieldRolename = (String) request.getParameter("Rolename");

				if (((FieldRolename != null) & (!FieldRolename.trim().equals("")))) {
					FieldRolename = FieldRolename.trim();
					meminfo.setRolename(FieldRolename);

				} else {
					tag = false;
					Msg = "角色名称不能为空！";
				}
			}

			if (tag) {

				String FieldCompanyname = (String) request.getParameter("Companyname");

				if (((FieldCompanyname != null) & (!FieldCompanyname.trim().equals("")))) {
					FieldCompanyname = FieldCompanyname.trim();
					meminfo.setCompanyname(FieldCompanyname);

				} else {
					tag = false;
					Msg = "公司名称不能为空！";
				}
			}

			if (tag) {

				String FieldPrivince = (String) request.getParameter("Privince");

				if (((FieldPrivince != null) & (!FieldPrivince.trim().equals("")))) {
					FieldPrivince = FieldPrivince.trim();
					meminfo.setPrivince(FieldPrivince);

				} else {
					tag = false;
					Msg = "所在省份不能为空！";
				}
			}

			if (tag) {

				String FieldCity = (String) request.getParameter("City");

				if (((FieldCity != null) & (!FieldCity.trim().equals("")))) {
					FieldCity = FieldCity.trim();
					meminfo.setCity(FieldCity);

				} else {
					tag = false;
					Msg = "所在市州不能为空！";
				}
			}

			if (tag) {

				String FieldDistrict = (String) request.getParameter("District");

				if (((FieldDistrict != null) & (!FieldDistrict.trim().equals("")))) {
					FieldDistrict = FieldDistrict.trim();
					meminfo.setDistrict(FieldDistrict);

				} else {
					tag = false;
					Msg = "所在县区不能为空！";
				}
			}

			if (tag) {

				String FieldContactman = (String) request.getParameter("Contactman");

				if (((FieldContactman != null) & (!FieldContactman.trim().equals("")))) {
					FieldContactman = FieldContactman.trim();
					meminfo.setContactman(FieldContactman);

				} else {
					tag = false;
					Msg = "联系人不能为空！";
				}
			}

			if (tag) {

				String FieldContacttel = (String) request.getParameter("Contacttel");

				if (((FieldContacttel != null) & (!FieldContacttel.trim().equals("")))) {
					FieldContacttel = FieldContacttel.trim();
					meminfo.setContacttel(FieldContacttel);

				} else {
					tag = false;
					Msg = "联系电话不能为空！";
				}
			}

			if (tag) {

				String FieldEmail = (String) request.getParameter("Email");

				if (((FieldEmail != null) & (!FieldEmail.trim().equals("")))) {
					FieldEmail = FieldEmail.trim();
					meminfo.setEmail(FieldEmail);

				} else {
					tag = false;
					Msg = "电子邮箱不能为空！";
				}
			}

			if (tag) {

				String FieldAddress = (String) request.getParameter("Address");

				if (((FieldAddress != null) & (!FieldAddress.trim().equals("")))) {
					FieldAddress = FieldAddress.trim();
					meminfo.setAddress(FieldAddress);

				} else {
					tag = false;
					Msg = "地址不能为空！";
				}
			}

			if (tag) {

				String FieldRemark = (String) request.getParameter("Remark");

				if (((FieldRemark != null) & (!FieldRemark.trim().equals("")))) {
					FieldRemark = FieldRemark.trim();
					meminfo.setRemark(FieldRemark);

				} else {
					tag = false;
					Msg = "备注不能为空！";
				}
			}
			meminfo.setStatus("未审核");

			MyJson mj = new MyJson();
			if (tag) {
				meminfoService.Save(meminfo);
				mj.setStatusCode(200);
			} else {
				mj.setStatusCode(300);
			}
			mj.setMessage(Msg);
			mj.setForwardUrl("/Meminfo/Wsh.do");
			mj.setCallbackType("forward");
			String str = mj.getJson();
			response.getWriter().print(str);
		} catch (Exception e) {
			logger.error(e.getMessage());
			e.printStackTrace();
		}
	}

	/* 编辑 */
	@RequestMapping("Edit.do")
	public String Edit(HttpSession session, HttpServletRequest request, ModelMap modelMap) {
		Meminfo smeminfo = (Meminfo) session.getAttribute("SessionMem");
		modelMap.put("smeminfo", smeminfo);

		List hylist = dictService.getAllByKeyname("角色");
		modelMap.put("hylist", hylist);
		String ID = request.getParameter("ID");
		Meminfo meminfo = (Meminfo) meminfoService.getMeminfo(ID);
		modelMap.put("meminfo", meminfo);
		return "/Meminfo/Edit";
	}

	/* 编辑保存 */
	@RequestMapping("EditSave.do")
	public void EditSave(HttpServletResponse response, HttpServletRequest request, ModelMap modelMap,
			HttpSession session) {
		Meminfo smeminfo = (Meminfo) session.getAttribute("SessionMem");
		modelMap.put("smeminfo", smeminfo);
		try {
			String Id = (String) request.getParameter("ID");
			Meminfo meminfo = (Meminfo) meminfoService.getMeminfo(Id);
			boolean tag = true;
			String Msg = "信息修改成功";

			if (tag) {

				String FieldMemname = (String) request.getParameter("Memname");

				if (((FieldMemname != null) & (!FieldMemname.trim().equals("")))) {
					FieldMemname = FieldMemname.trim();
					meminfo.setMemname(FieldMemname);

				} else {
					tag = false;
					Msg = "会员名称不能为空！";
				}
			}

			if (tag) {

				String FieldMempass = (String) request.getParameter("Mempass");

				if (((FieldMempass != null) & (!FieldMempass.trim().equals("")))) {
					FieldMempass = FieldMempass.trim();
					meminfo.setMempass(FieldMempass);

				} else {
					tag = false;
					Msg = "会员密码不能为空！";
				}
			}

			if (tag) {

				String FieldRolename = (String) request.getParameter("Rolename");

				if (((FieldRolename != null) & (!FieldRolename.trim().equals("")))) {
					FieldRolename = FieldRolename.trim();
					meminfo.setRolename(FieldRolename);

				} else {
					tag = false;
					Msg = "角色名称不能为空！";
				}
			}

			if (tag) {

				String FieldCompanyname = (String) request.getParameter("Companyname");

				if (((FieldCompanyname != null) & (!FieldCompanyname.trim().equals("")))) {
					FieldCompanyname = FieldCompanyname.trim();
					meminfo.setCompanyname(FieldCompanyname);

				} else {
					tag = false;
					Msg = "公司名称不能为空！";
				}
			}

			if (tag) {

				String FieldPrivince = (String) request.getParameter("Privince");

				if (((FieldPrivince != null) & (!FieldPrivince.trim().equals("")))) {
					FieldPrivince = FieldPrivince.trim();
					meminfo.setPrivince(FieldPrivince);

				} else {
					tag = false;
					Msg = "所在省份不能为空！";
				}
			}

			if (tag) {

				String FieldCity = (String) request.getParameter("City");

				if (((FieldCity != null) & (!FieldCity.trim().equals("")))) {
					FieldCity = FieldCity.trim();
					meminfo.setCity(FieldCity);

				} else {
					tag = false;
					Msg = "所在市州不能为空！";
				}
			}

			if (tag) {

				String FieldDistrict = (String) request.getParameter("District");

				if (((FieldDistrict != null) & (!FieldDistrict.trim().equals("")))) {
					FieldDistrict = FieldDistrict.trim();
					meminfo.setDistrict(FieldDistrict);

				} else {
					tag = false;
					Msg = "所在县区不能为空！";
				}
			}

			if (tag) {

				String FieldContactman = (String) request.getParameter("Contactman");

				if (((FieldContactman != null) & (!FieldContactman.trim().equals("")))) {
					FieldContactman = FieldContactman.trim();
					meminfo.setContactman(FieldContactman);

				} else {
					tag = false;
					Msg = "联系人不能为空！";
				}
			}

			if (tag) {

				String FieldContacttel = (String) request.getParameter("Contacttel");

				if (((FieldContacttel != null) & (!FieldContacttel.trim().equals("")))) {
					FieldContacttel = FieldContacttel.trim();
					meminfo.setContacttel(FieldContacttel);

				} else {
					tag = false;
					Msg = "联系电话不能为空！";
				}
			}

			if (tag) {

				String FieldEmail = (String) request.getParameter("Email");

				if (((FieldEmail != null) & (!FieldEmail.trim().equals("")))) {
					FieldEmail = FieldEmail.trim();
					meminfo.setEmail(FieldEmail);

				} else {
					tag = false;
					Msg = "电子邮箱不能为空！";
				}
			}

			if (tag) {

				String FieldAddress = (String) request.getParameter("Address");

				if (((FieldAddress != null) & (!FieldAddress.trim().equals("")))) {
					FieldAddress = FieldAddress.trim();
					meminfo.setAddress(FieldAddress);

				} else {
					tag = false;
					Msg = "地址不能为空！";
				}
			}

			if (tag) {

				String FieldRemark = (String) request.getParameter("Remark");

				if (((FieldRemark != null) & (!FieldRemark.trim().equals("")))) {
					FieldRemark = FieldRemark.trim();
					meminfo.setRemark(FieldRemark);

				} else {
					tag = false;
					Msg = "备注不能为空！";
				}
			}

			MyJson mj = new MyJson();
			if (tag) {
				meminfoService.EditSave(meminfo);
				mj.setStatusCode(200);
			} else {
				mj.setStatusCode(300);
			}
			mj.setMessage(Msg);
			mj.setForwardUrl("/Meminfo/Wsh.do");
			mj.setCallbackType("forward");
			String str = mj.getJson();
			response.getWriter().print(str);
		} catch (Exception e) {
			logger.error(e.getMessage());
			e.printStackTrace();
		}
	}

	/* 删除 */
	@RequestMapping("Del.do")
	public void Del(HttpSession session, HttpServletResponse response, HttpServletRequest request, ModelMap modelMap) {
		Meminfo smeminfo = (Meminfo) session.getAttribute("SessionMem");
		modelMap.put("smeminfo", smeminfo);

		String ID = request.getParameter("ID");
		meminfoService.Del(ID);
		String Msg = "数据删除成功";
		MyJson mj = new MyJson();
		mj.setStatusCode(200);
		mj.setMessage(Msg);
		mj.setForwardUrl("/Meminfo/Wsh.do");
		mj.setCallbackType("forward");
		String str = mj.getJson();
		try {
			response.getWriter().print(str);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/* 审核 */
	@RequestMapping("Sh.do")
	public void Sh(HttpSession session, HttpServletResponse response, HttpServletRequest request, ModelMap modelMap) {
		Meminfo smeminfo = (Meminfo) session.getAttribute("SessionMem");
		modelMap.put("smeminfo", smeminfo);

		String ID = request.getParameter("ID");
		Meminfo mem = meminfoService.getMeminfo(ID);
		mem.setStatus("已审核");
		meminfoService.EditSave(mem);
		String Msg = "审核成功";
		MyJson mj = new MyJson();
		mj.setStatusCode(200);
		mj.setMessage(Msg);
		mj.setForwardUrl("/Meminfo/Wsh.do");
		mj.setCallbackType("forward");
		String str = mj.getJson();
		try {
			response.getWriter().print(str);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/* 编辑 */
	@RequestMapping("Detail.do")
	public String Detail(HttpSession session, HttpServletRequest request, ModelMap modelMap) {
		Meminfo smeminfo = (Meminfo) session.getAttribute("SessionMem");
		modelMap.put("smeminfo", smeminfo);

		String ID = request.getParameter("ID");
		Meminfo meminfo = (Meminfo) meminfoService.getMeminfo(ID);
		modelMap.put("meminfo", meminfo);
		return "/Meminfo/Detail";
	}

	@RequestMapping("ChangePasswd.do")
	public void ChangePasswd(HttpSession session, HttpServletResponse response, HttpServletRequest request,
			ModelMap modelMap) {
		Meminfo smeminfo = (Meminfo) session.getAttribute("SessionMem");
		modelMap.put("smeminfo", smeminfo);
		String str = "";
		String ID = request.getParameter("ID");
		String oldPasswd = request.getParameter("oldPasswd");
		String newPasswd = request.getParameter("newPasswd");
		String Passwd = request.getParameter("Passwd");
		Meminfo user = (Meminfo) meminfoService.getMeminfo(ID);
		if ((oldPasswd != null) && (getMD5(oldPasswd).equals(user.getMempass()))) {
			if (newPasswd.equals(Passwd)) {
				str = "{\"statusCode\":200,\"message\":\""
						+ "密码修改成功\",\"callbackType\":\"forward\",\"forwardUrl\":\"/Meminfo/Passwd.do\"}";
				user.setMempass(getMD5(Passwd));
				meminfoService.EditSave(user);
				session.setAttribute("SessionUser", user);
			} else {
				str = "{\"statusCode\":300,\"message\":\""
						+ "两次新密码不一致，密码修改失败\",\"callbackType\":\"forward\",\"forwardUrl\":\"\"}";
			}
		} else {
			str = "{\"statusCode\":300,\"message\":\""
					+ "旧密码不匹配，密码修改失败\",\"callbackType\":\"forward\",\"forwardUrl\":\"\"}";
		}
		try {
			response.getWriter().print(str);
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
	}

	/* 修改密码 */
	@RequestMapping("Passwd.do")
	public String Passwd(ModelMap modelMap, HttpSession session) {
		Meminfo smeminfo = (Meminfo) session.getAttribute("SessionMem");
		modelMap.put("smeminfo", smeminfo);
		return "/Meminfo/Passwd";
	}

	public static String getMD5(String str) {
		try {
			// 生成一个MD5加密计算摘要
			MessageDigest md = MessageDigest.getInstance("MD5");
			// 计算md5函数
			md.update(str.getBytes());
			// digest()最后确定返回md5 hash值，返回值为8为字符串。因为md5 hash值是16位的hex值，实际上就是8位的字符
			// BigInteger函数则将8位的字符串转换成16位hex值，用字符串来表示；得到字符串形式的hash值
			return new BigInteger(1, md.digest()).toString(16);
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
			return null;
		}
	}
}
